package rsee.com;
import com.video.h264.GlobalUtil;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PushActivity extends Activity {
	private String mDeviceID;
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
 			super.handleMessage(msg);
 			int what = msg.what;
 			switch(what)
 			{    
 			    case 0:	
 			  		startButton.setEnabled(false);
 			  		stopButton.setEnabled(true);
 					PushService.actionStart(getApplicationContext());	
 			    	break;
 			    case 1:
 			  		startButton.setEnabled(true);
 			  		stopButton.setEnabled(false);
 					PushService.actionStop(getApplicationContext());
 			    	break;
 			}
		}
	};
	 Button startButton;
	 Button stopButton;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pushset);
        
        mDeviceID = getSharedPreferences(PushService.TAG, MODE_PRIVATE).getString(PushService.PREF_DEVICE_ID, null);		//Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);         
  	  	((TextView) findViewById(R.id.target_text)).setText(mDeviceID);
 
  	  startButton = ((Button) findViewById(R.id.start_button));
  	  stopButton  = ((Button) findViewById(R.id.stop_button));
  	  	startButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
//		    	Editor editor = getSharedPreferences(PushService.TAG, MODE_PRIVATE).edit();
//		    	editor.putString(PushService.PREF_DEVICE_ID, mDeviceID);
//		    	editor.commit();
//				PushService.actionStart(getApplicationContext());	
				handler.sendEmptyMessage(0);
				
			}
		});
  	  	stopButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				PushService.actionStop(getApplicationContext());
				handler.sendEmptyMessage(1);				
			}
		});
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
  	  	
  	  	SharedPreferences p = getSharedPreferences(PushService.TAG, MODE_PRIVATE);
  	  	boolean started = p.getBoolean(PushService.PREF_STARTED, false);
  	  	
  		((Button) findViewById(R.id.start_button)).setEnabled(!started);
  		((Button) findViewById(R.id.stop_button)).setEnabled(started);
  		
			if(GlobalUtil.OVSIAlarmConfig){
				ActivityList activityList = new ActivityList();
				activityList.put(this, 0);
				}
    }
    
    @Override
	protected void onDestroy() {
        super.onDestroy();
		if(GlobalUtil.OVSIAlarmConfig){
			ActivityList activityList = new ActivityList();
			activityList.remove(this);
			}
    }
}